package com.kwin.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class CheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //内存中生成一张图片
        BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
        //获取到这张图片
        Graphics graphics = bufferedImage.getGraphics();
        //设置颜色字体
        graphics.setColor(Color.WHITE);
        //往图片中写数据
        graphics.drawString("1234", 0, 20);
        //要往浏览器写一张拖欠，告诉浏览器需要回送的类型是一张图片
        resp.setHeader("ContentType", "jpeg");
        //图片传进去，类型是jpg，写给浏览器
        ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
